#include "Upload_database.h"
#include "list.h"
#include"sys_log.h"

//连接mysql
MYSQL* connect_mysql() {
	MYSQL* connect = NULL;
	MYSQL* res = NULL;
	//连接数据库
	connect = mysql_init(NULL);
	// 设置连接字符集为utf8mb4
	if (mysql_options(connect, MYSQL_SET_CHARSET_NAME, "utf8mb4") != 0) {
		fprintf(stderr, "mysql_options() failed\n");
		return NULL;
	}


	res = mysql_real_connect(connect, "localhost", "root", password, "Downhole_Water_Pumps", 0, NULL, 0);
	if (res == NULL) {
		printf("%s", mysql_error(connect));
		exit(-1);
	}
	printf("\n连接mysql成功！\n");
	printf("\n");
	return connect;
}


//读取管理员数据并插入数据库
void data_insert_database(USE* data, MYSQL* connect) {
	char insert_mysql[100] = { 0 };
	sprintf(insert_mysql, "insert into admin values(%d,'%s',%s)", data->user_id, data->user_name, data->user_password);
	int ret = 0;
	ret = mysql_query(connect, insert_mysql);
	if (ret != 0)
	{
		printf("%s", mysql_error(connect));
	}
	printf("管理员信息插入数据库成功\n");
	printf("\n");
}

//读取用户数据并插入数据库
void data_insert_database2(USE* data, MYSQL* connect) {
	char insert_mysql[100] = { 0 };
	sprintf(insert_mysql, "insert into user_info values(%d,'%s',%s)", data->user_id, data->user_name, data->user_password);
	int ret = 0;
	ret = mysql_query(connect, insert_mysql);
	if (ret != 0)
	{
		printf("%s", mysql_error(connect));
	}
	printf("用户信息插入数据库成功\n");
	printf("\n");
}



//读取数据库里面的管理员信息到链表当中
USE* read_mysql_to_list(MYSQL* connect, USE* head) {//重新读取到新链表中，应该返回新链表头结点
	MYSQL_RES* res = NULL;//RES拿结果
	MYSQL_ROW   row = NULL;//ROW拿每一行
	USE* p = head;
	//应该是先读取！！！
	char select_sql[100] = { 0 };
	sprintf(select_sql, "select * from admin");
	//读取成功就是0
	int ret = 0;
	ret = mysql_query(connect, select_sql);
	if (ret != 0)
	{
		printf("%s", mysql_error(connect));
	}

	MYSQL_RES* damie = NULL;//接收数据集的类型也必须是MYSQL_RES
	damie = mysql_store_result(connect);//拿到结果集，放在damie
	if (damie == NULL) {
		printf("%s\n", mysql_error(connect));
		mysql_close(connect);
		exit(-1);
	}
	row = mysql_fetch_row(damie);//按行读取damie



	//--------------------获取MySQL字段名--------------------------
	int rows = 0;
	int j = 0;
	int flag = 0;
	MYSQL_FIELD* my_field;
	//

	while (row != NULL) {

		//--------------------获取MySQL字段名---------------------------     
		if (flag == 0) {
			my_field = mysql_fetch_fields(damie);
			flag = 1;
		}

		//先申请节点再连接!!!
		USE* newnode = (USE*)malloc(sizeof(USE));
		memset(newnode, 0, sizeof(USE));

		sscanf(row[0], "%d", &newnode->user_id);//需要掌握sscanf的用法
		sscanf(row[1], "%s", newnode->user_name);
		sscanf(row[2], "%s", newnode->user_password);
		p->Next = newnode;
		newnode->Next = NULL;
		p = newnode;
		row = mysql_fetch_row(damie);
	}
	return head;
}


//读取数据库里面的用户信息到链表当中
USE* read_mysql_to_list2(MYSQL* connect, USE* head) {//重新读取到新链表中，应该返回新链表头结点
	MYSQL_RES* res = NULL;//RES拿结果
	MYSQL_ROW   row = NULL;//ROW拿每一行
	USE* p = head;
	//应该是先读取！！！
	char select_sql[100] = { 0 };
	sprintf(select_sql, "select * from user_info");
	//读取成功就是0
	int ret = 0;
	ret = mysql_query(connect, select_sql);
	if (ret != 0)
	{
		printf("%s", mysql_error(connect));
	}

	MYSQL_RES* damie = NULL;//接收数据集的类型也必须是MYSQL_RES
	damie = mysql_store_result(connect);//拿到结果集，放在damie
	if (damie == NULL) {
		printf("%s\n", mysql_error(connect));
		mysql_close(connect);
		exit(-1);
	}
	row = mysql_fetch_row(damie);//按行读取damie



	//--------------------获取MySQL字段名--------------------------
	int rows = 0;
	int j = 0;
	int flag = 0;
	MYSQL_FIELD* my_field;
	//

	while (row != NULL) {

		//--------------------获取MySQL字段名---------------------------     
		if (flag == 0) {
			my_field = mysql_fetch_fields(damie);
			flag = 1;
		}

		//先申请节点再连接!!!
		USE* newnode = (USE*)malloc(sizeof(USE));
		memset(newnode, 0, sizeof(USE));

		sscanf(row[0], "%d", &newnode->user_id);//需要掌握sscanf的用法
		sscanf(row[1], "%s", newnode->user_name);
		sscanf(row[2], "%s", newnode->user_password);
		p->Next = newnode;
		newnode->Next = NULL;
		p = newnode;
		row = mysql_fetch_row(damie);
	}
	return head;
}








//把水泵数据插入数据库------------水泵的1号功能插入使用
int pump_data_insert_database3(WP* data, MYSQL* connect) {
	int flag = 0;
	char insert_mysql[100] = { 0 };
	sprintf(insert_mysql, "insert into water_pump values(%d,'%s')",data->id,data->pump_name);
	int ret = 0;
	ret = mysql_query(connect, insert_mysql);
	if (ret != 0)
	{
		printf("%s", mysql_error(connect));
	}
	printf("水泵信息插入数据库成功\n");
	flag = 1;

	return flag;
}

//水泵的2号功能删除使用
int pump_data_delete_database3(WP* data, MYSQL* connect) {
	int flag = 0;
	char delete_mysql[100] = { 0 };
	sprintf(delete_mysql, "delete from water_pump where pump_name = '%s'", data->pump_name);
	int ret = 0;
	ret = mysql_query(connect, delete_mysql);
	if (ret != 0)
	{
		printf("%s", mysql_error(connect));
	}
	printf("该水泵信息删除数据库成功\n");
	flag = 1;

	return flag;
}



//把水泵数据读取到链表当中-----------水泵的3号功能查询使用
WP* read_mysql_to_list3(MYSQL* connect, WP* head) {//重新读取到新链表中，应该返回新链表头结点
	MYSQL_RES* res = NULL;//RES拿结果
	MYSQL_ROW   row = NULL;//ROW拿每一行
	WP* p = head;
	//应该是先读取！！！
	char select_sql[100] = { 0 };
	sprintf(select_sql, "select * from water_pump");
	//读取成功就是0
	int ret = 0;
	ret = mysql_query(connect, select_sql);
	if (ret != 0)
	{
		printf("%s", mysql_error(connect));
	}

	MYSQL_RES* damie = NULL;//接收数据集的类型也必须是MYSQL_RES
	damie = mysql_store_result(connect);//拿到结果集，放在damie
	if (damie == NULL) {
		printf("%s\n", mysql_error(connect));
		mysql_close(connect);
		exit(-1);
	}
	row = mysql_fetch_row(damie);//按行读取damie


	//--------------------获取MySQL字段名--------------------------
	int rows = 0;
	int j = 0;
	int flag = 0;
	MYSQL_FIELD* my_field;
	//

	while (row != NULL) {

		//--------------------获取MySQL字段名---------------------------     
		if (flag == 0) {
			my_field = mysql_fetch_fields(damie);
			flag = 1;
		}

		//先申请节点再连接!!!
		WP* newnode = (WP*)malloc(sizeof(WP));
		memset(newnode, 0, sizeof(WP));

		sscanf(row[0], "%d", &newnode->id);//需要掌握sscanf的用法
		sscanf(row[1], "%s", newnode->pump_name);
		p->Next = newnode;
		newnode->Next = NULL;
		p = newnode;
		row = mysql_fetch_row(damie);
	}
	return head;
}



//把具体数据读取到链表当中-具体数据的1号功能查询使用
DAT* read_mysql_to_data_list(MYSQL* connect, DAT* head) {
	// 初始化MySQL结果集和行数据
	MYSQL_RES* res = NULL;
	MYSQL_ROW row = NULL;
	DAT* p = head;

	// 准备SQL查询语句
	char select_sql[100] = { 0 };
	sprintf(select_sql, "select * from details");
	printf("Executing SQL query: %s\n", select_sql);  // 调试信息

	// 执行查询
	int ret = mysql_query(connect, select_sql);
	if (ret != 0) {
		printf("SQL query failed: %s\n", mysql_error(connect));  // 错误调试信息
		return head;
	}

	// 获取查询结果集
	MYSQL_RES* damie = mysql_store_result(connect);
	if (damie == NULL) {
		printf("Failed to store result set: %s\n", mysql_error(connect));  // 错误调试信息
		mysql_close(connect);
		exit(-1);
	}
	printf("Result set retrieved successfully.\n");  // 调试信息

	// 逐行读取结果
	row = mysql_fetch_row(damie);
	int row_count = 0;  // 用于调试的行计数
	while (row != NULL) {
		row_count++;
		printf("Processing row %d\n", row_count);  // 调试信息

		// 创建新节点并初始化
		DAT* newnode = (DAT*)malloc(sizeof(DAT));
		memset(newnode, 0, sizeof(DAT));

		// 将数据解析并存入新节点
		sscanf(row[1], "%f", &newnode->temperature);
		sscanf(row[2], "%f", &newnode->liquid_level);
		sscanf(row[3], "%f", &newnode->flow_rate);
		sscanf(row[4], "%d", &newnode->valve_state);
		sscanf(row[5], "%f", &newnode->negative_pressure);
		sscanf(row[6], "%f", &newnode->pressure);
		sscanf(row[7], "%s", newnode->time);
		sscanf(row[8], "%d", &newnode->pump_id);

		// 输出解析后的每一行数据
		printf("Row %d data: temp=%.2f, liquid=%.2f, flow=%.2f, valve=%d, negative=%.2f, pressure=%.2f, time=%s, pump_id=%d\n",
			row_count, newnode->temperature, newnode->liquid_level, newnode->flow_rate, newnode->valve_state,
			newnode->negative_pressure, newnode->pressure, newnode->time, newnode->pump_id);

		// 链接新节点到链表
		p->Next = newnode;
		newnode->Next = NULL;
		p = newnode;

		// 读取下一行
		row = mysql_fetch_row(damie);
	}

	printf("Total rows processed: %d\n", row_count);  // 总计行数的调试信息
	return head;
}



















//这是循环发客户端的功能
int data_insert_database4(DAT* data, MYSQL* connect) {
	int flag = 0;
	char insert_mysql[1024] = { 0 };
	sprintf(insert_mysql, "INSERT INTO details (temperature, liquid_level, flow_rate, valve_state, negative_pressure, pressure, time, pump_id) values(%f,%f,%f,%d,%f,%f,'%s',%d); ", data->temperature, data->liquid_level, data->flow_rate, data->valve_state, data->negative_pressure, data->pressure, data->time, data->pump_id);
	int ret = 0;
	ret = mysql_query(connect, insert_mysql);
	if (ret != 0)
	{
		printf("%s", mysql_error(connect));
	}
	flag = 1;

	return flag;


}
